/*
 * @(#)DrawConfigImpl.java   09/12/29
 * 
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 */



package fr.ema.arch3d.editor.config;

import fr.ema.arch3d.editor.port.MyCellViewFactory;
import fr.ema.arch3d.jmodel.configuration.Configuration;
import fr.ema.arch3d.jmodel.configuration.DefaultConfiguration;
import fr.ema.arch3d.xoadl.configuration.CompositeComponentClassADL;
import fr.ema.arch3d.xoadl.configuration.ConfigurationADL.ComponentImplementation;
import fr.ema.arch3d.xoadl.configuration.PrimitiveComponentClassADL;

import org.jgraph.JGraph;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;

import org.objectweb.fractal.api.NoSuchInterfaceException;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextPane;

//~--- classes ----------------------------------------------------------------

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class DrawConfigImpl {

    /** Field description */
    public static JScrollPane cGraph;

    /** Field description */
    public static JTextPane cText;

    /** Field description */
    public static JScrollPane cTree;

    /** Field description */
    public Configuration config;

    /** Field description */
    public JGraph thisGraph;

    /**
     * Method description
     *
     * @param config
     * @return
     */
    public JSplitPane drawConfig(Configuration config) {
        this.config = config;

        // TODO Auto-generated method stub
        GraphModel         model = new DefaultGraphModel();
        GraphLayoutCache   view  = new GraphLayoutCache(model, (new MyCellViewFactory()));
        JGraph             graph = new JGraph(model, view);
        DefaultGraphCell[] cells = null;

        // tree
        // draw vertext
        DrawVertex dv = new DrawVertex();

        try {
            List<PrimitiveComponentClassADL> cmpPs  = config.getPrimitiveComponentClass();
            List<CompositeComponentClassADL> cmpCs  = config.getCompositeComponentClass();
            List<ComponentImplementation>    cimpls = config.getComponentImplementation();

            cells = new DefaultGraphCell[cmpPs.size() + cmpCs.size()];

            String cmpRole = null;

            for (int i = 0; i < cmpPs.size(); i++) {
                for (int j = 0; j < cimpls.size(); j++) {
                    if (cimpls.get(j).getComponentClassName().equals(
                            ((PrimitiveComponentClassADL) cmpPs.get(i)).getName())) {
                        cmpRole = cimpls.get(j).getImplComponentRoleName();
                    }
                }

                cells[i] = dv.drawVertext((PrimitiveComponentClassADL) cmpPs.get(i), cmpRole, i);

                // GraphConstants.setAutoSize(cells[i].getAttributes(),true);
            }

            for (int i = 0; i < cmpCs.size(); i++) {
                for (int j = 0; j < cimpls.size(); j++) {
                    if (cimpls.get(j).getComponentClassName().equals(
                            ((CompositeComponentClassADL) cmpCs.get(i)).getName())) {
                        cmpRole = cimpls.get(j).getImplComponentRoleName();
                    }
                }

                cells[i] = dv.drawVertext((CompositeComponentClassADL) cmpCs.get(i), cmpRole, i + cmpPs.size());

                // GraphConstants.setAutoSize(cells[i].getAttributes(),true);
            }
        } catch (NoSuchInterfaceException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // draw edges
        DrawEdge                             de              = new DrawEdge();
        List<Configuration.ConfigConnection> connection_list = config.getConfigConnection();
        ConfigEdge[]                         edges           = null;
        if (connection_list != null) {
            edges = new ConfigEdge[connection_list.size()];
            for (int i = 0; i < edges.length; i++) {
                edges[i] = de.drawEdge(cells, connection_list.get(i));
            }
        }
        
        graph.getGraphLayoutCache().insert(cells);
        graph.getGraphLayoutCache().insert(edges);
        graph.setPortsVisible(true);
        cGraph = new JScrollPane(graph);

        ConfigTree tree = new ConfigTree(model);

        // graph.getModel().addGraphModelListener(gtModel);
        // tree.setRootVisible(false);
        cTree = new JScrollPane(tree.s);

        ConfigADLText t = new ConfigADLText();

        cText = new JTextPane();
//        cText.setDocument(t.createTextConfig(config));
        cText.setEditable(false);

        JSplitPane pane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, cTree, cGraph);
        JSplitPane pane  = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pane1, cText);

        graph.addMouseListener(new ConfigMouseEvent(this));
        pane.setSize(1100, 300);
        pane1.setDividerLocation(180);
        pane.setDividerLocation(900);
        thisGraph = graph;

        return pane;
    }
}
